package com.bitejiuyeke.job_publish.config;


import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author 比特就业课
 */
// RabbitMQ配置类
@Configuration
public class RabbitMQConfig {
    // Exchange名
    public static final String BITE_JOB_EXCHANGE_NAME = "BITE_JOB_EXCHANGE";
    // 短信队列名
    public static final String BITE_JOB_V_CODE_QUEUE_NAME = "BITE_JOB_V_CODE_QUEUE_NAME";
    // 短信RoutingKey
    public static final String BITE_JOB_V_CODE_ROUTING_KEY = "BITE_JOB_V_CODE_ROUTING_KEY";
    // OFFER队列名
    public static final String BITE_JOB_OFFERED_QUEUE_NAME = "BITE_JOB_OFFERED_QUEUE";
    // OFFER RoutingKey
    public static final String BITE_JOB_OFFERED_ROUTING_KEY = "BITE_JOB_OFFERED_ROUTING";

    /**
     * 构建短信消息队列
     * @return Queue
     */
    @Bean("smsQueue")
    public Queue smsQueue () {
        return QueueBuilder.durable(BITE_JOB_V_CODE_QUEUE_NAME).build();
    }

    /**
     * 构建已发Offer消息队列
     * @return Queue
     */
    @Bean("offeredQueue")
    public Queue offeredQueue () {
        return QueueBuilder.durable(BITE_JOB_OFFERED_QUEUE_NAME).build();
    }

    /**
     * 构建交换机
     * @return DirectExchange
     */
    @Bean("directExchange")
    public DirectExchange directExchange () {
        return ExchangeBuilder.directExchange(BITE_JOB_EXCHANGE_NAME).durable(true).build();
    }

    /**
     * 绑定短信队列和交换机，并指定ROUTING_KEY
     * @param directExchange 交换机
     * @param queue 队列
     * @return Binding
     */
    @Bean
    public Binding directBindingSMS (@Qualifier("directExchange") DirectExchange directExchange,
                                     @Qualifier("smsQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with(BITE_JOB_V_CODE_ROUTING_KEY);
    }

    /**
     * 绑定OFFERED队列和交换机，并指定ROUTING_KEY
     * @param directExchange 交换机
     * @param queue 队列
     * @return Binding
     */
    @Bean
    public Binding directBindingOffered (@Qualifier("directExchange") DirectExchange directExchange,
                                         @Qualifier("offeredQueue") Queue queue) {

        return BindingBuilder.bind(queue).to(directExchange).with(BITE_JOB_OFFERED_ROUTING_KEY);
    }

    /**
     * 设置消息转换对象
     * @return MessageConverter
     */
    @Bean
    public MessageConverter jackson2JsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitTemplate rabbitTemplate (ConnectionFactory connectionFactory) {
        // 创建对象
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        // 设置消息转换器
        rabbitTemplate.setMessageConverter(jackson2JsonMessageConverter());
        return rabbitTemplate;
    }


}
